package com.thoughtworks.retailstore.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.thoughtworks.models.CartModel;
import com.thoughtworks.models.ProductModel;
import com.thoughtworks.retailstore.R;
import com.thoughtworks.retailstore.utils.Constants;

// TODO: Auto-generated Javadoc
/**
 * The Class ProductDetailsFragment.
 */
public class ProductDetailsFragment extends Fragment 
{

	/** The price tv. */
	private TextView prodNameTv, priceTv ;

	/** The child pos. */
	private int grpPos , childPos ;

	/** The image. */
	private ImageView image;

	/** The add to cart btn. */
	private Button addToCartBtn ;


	/**
	 * New instance.
	 *
	 * @param _groupPos the _group pos
	 * @param _childPos the _child pos
	 * @return the product details fragment
	 */
	public static ProductDetailsFragment newInstance(int _groupPos, int _childPos)
	{
		ProductDetailsFragment f = new ProductDetailsFragment();
		Bundle b = new Bundle();
		b.putInt("group_position", _groupPos);
		b.putInt("child_position",_childPos);
		f.setArguments(b);
		return f;
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle args) 
	{
		View view = inflater.inflate(R.layout.product_details_layout, null);

		initViews(view);

		grpPos = getArguments().getInt("group_position");
		childPos = getArguments().getInt("child_position");

		setData();

		addToCartBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) 
			{
				ProductModel product = new ProductModel();
				product.setProductName(Constants.categoryList.get(grpPos)
						.getProdList().get(childPos).getProductName());
				product.setProductPrice(Constants.categoryList.get(grpPos)
						.getProdList().get(childPos).getProductPrice());
				product.setCategId(Constants.categoryList.get(grpPos)
						.getProdList().get(childPos).getCategId());
				product.setProdId(Constants.categoryList.get(grpPos)
						.getProdList().get(childPos).getProdId());

				CartModel cart = CartModel.getInstance() ;
				cart.addToCart(product);

				CartDetailsFragment cartFrag = CartDetailsFragment.newInstance();
				FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
				ft.replace(R.id.container, cartFrag, "cart_fragment");
				ft.addToBackStack("cart_fragment");
				ft.commit();
			}
		});

		return view;
	}

	/**
	 * Inits the views.
	 *
	 * @param view the view
	 */
	public void initViews(View view)
	{
		try{
			prodNameTv = (TextView)view.findViewById(R.id.prodNameTxtView);
			priceTv = (TextView)view.findViewById(R.id.priceTextView);
			image = (ImageView)view.findViewById(R.id.prodImgView);
			addToCartBtn = (Button)view.findViewById(R.id.addToCartBtn);
		}
		catch(Exception e){
			e.printStackTrace();
		}

	}

	/**
	 * Sets the data.
	 */
	public void setData()
	{
		try{
			String prodName = Constants.categoryList.get(grpPos).getProdList().get(childPos).getProductName();
			prodNameTv.setText(prodName);

			int price = Constants.categoryList.get(grpPos).getProdList().get(childPos).getProductPrice();
			priceTv.setText(Integer.toString(price)+" INR");

			int imageId = Constants.categoryList.get(grpPos).getProdList().get(childPos).getProductImgId();
			image.setImageResource(imageId);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
